const path = require('path');
const webpack = require('webpack');
const BASE_URL = process.env.NODE_ENV === 'production' ? '/' : '/';
const IS_PROD = ['production', 'prod'].includes(BASE_URL);//检测构建环境
const CompressionWebpackPlugin = require('compression-webpack-plugin');
const productionGzipExtensions = /\.(js|css|json|txt|html|ico|svg)(\?.*)?$/i;

// const assetsCDN = {
//   // webpack build externals
//   externals: {
//     vue: 'Vue',
//     'vue-router': 'VueRouter',
//     vuex: 'Vuex',
//     axios: 'axios',
//     'element-ui': 'ELEMENT',
//   },
//   css: [
//     '//cdn.jsdelivr.net/npm/element-ui@2.14.1/lib/theme-chalk/index.css'
//   ],
//   // https://unpkg.com/browse/vue@2.6.10/

//   js: [
//     '//cdn.jsdelivr.net/npm/vue@2.6.10/dist/vue.min.js',
//     '//cdn.jsdelivr.net/npm/vue-router@3.1.3/dist/vue-router.min.js',
//     '//cdn.jsdelivr.net/npm/vuex@3.1.1/dist/vuex.min.js',
//     '//cdn.jsdelivr.net/npm/axios@0.19.0/dist/axios.min.js',
//     '//cdn.jsdelivr.net/npm/element-ui@2.14.1/lib/index.js'
//   ]
// }
const resolve = dir => path.join(__dirname, dir);
module.exports = {
  // publicPath: './',
  publicPath: BASE_URL,
  productionSourceMap: false,// 打包时不生成.map文件
  lintOnSave: false,//关闭本地调试代码格式效验、
  outputDir: 'dist',
  assetsDir: 'staticDist',

  configureWebpack: {
    plugins: [
      // Ignore all locale files of moment.js
      new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
      new CompressionWebpackPlugin({
        filename: '[path].gz[query]',
        algorithm: 'gzip',
        test: productionGzipExtensions,
        threshold: 10240, // 对超过10k的数据进行压缩
        minRatio: 0.8, // 只有压缩率小于这个值的资源才会被处理
        deleteOriginalAssets: false, // 删除原文件
      }),
    ],
    // externals: BASE_URL ? assetsCDN.externals : {}
  },
  chainWebpack: (config) => {
    config.resolve.alias
      .set('@', resolve('src')) // key,value自行定义，比如.set('@@', resolve('src/components'))
      .set('@Decoration', resolve('src/view/Decoration/common'))
    // if (BASE_URL) {
    //   config.plugin('html').tap(args => {
    //     args[0].cdn = assetsCDN
    //     return args
    //   })
    // }
  },
  devServer: {
    proxy: {
      "/api": {
        // target: "https://api.ryz1620.com/", //服务器
        target: "https://lkserver.littlegood.cn/", //服务器
        // target: "http://lk-server.com/", //本地
        changeOrigin: true,
        pathRewrite: {
          '^/api': ''
        }
      },
    },
    // historyApiFallback: true
  },
};
